package com.orm.base;

import com.alibaba.fastjson.JSONObject;
import com.orm.anno.FieldName;
import com.orm.base.util.ClassUtil;
import com.orm.base.wrapper.MyLambdaQueryWrapper;
import com.orm.anno.TableName;
import com.orm.base.util.SqlGenUtil;
import com.orm.config.JdbcCache;
import com.orm.config.JdbcTemplateConfig;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
@Data
public class DefaultMyMapperImpl implements MyMapper{

    private Class interfaceClass;

    private Class entityClass;

    private String tableName;

    public  DefaultMyMapperImpl(Class interfaceClass) {
        this.entityClass = ClassUtil.getGenericClass(interfaceClass);
        this.tableName = getTableName(interfaceClass);
    }

    @Override
    public Object selectById(Long id) {
        // 获取实体类的表名
        String tableName = getTableName(interfaceClass);
        // 构造 SQL
        String sql = "select * from " + tableName + " where id = " + id;
        JdbcTemplate jdbcTemplate = JdbcCache.getJdbcTemplate();
        return buildResult(jdbcTemplate.queryForMap(sql), entityClass);
    }

    @Override
    public Object deleteById(Long id) {
        // 获取实体类的表名
        String tableName = getTableName(interfaceClass);
        String sql = "delete from " + tableName + " where id = " + id;
        System.out.println("Executing SQL: " + sql);
        return null;
    }

    @Override
    public List selectList(MyLambdaQueryWrapper queryWrapper) {
        String sql = SqlGenUtil.genQuerySql(queryWrapper,tableName);
        System.out.println("Executing SQL: " + sql);
        return new ArrayList();
    }

    @SneakyThrows
    private Object buildResult(Map<String, Object> dbResultMap, Class<?> entityClass) {
        if (dbResultMap.isEmpty()) {
            return null;
        }
        Object entity = entityClass.newInstance();
        Field[] fields = entityClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(FieldName.class)) {
                FieldName dbFieldAnno = field.getAnnotation(FieldName.class);
                String dbFieldName = dbFieldAnno.value();
                // 设置字段的可访问性（因为 name 字段是私有的）
                field.setAccessible(true);
                // TODO 要字段类型转换
                field.set(entity, dbResultMap.get(dbFieldName).toString());
            }
        }
        return entity;
    }



    /**
     * 获取表名
     *
     * @param interfaceClass 被代理的 interface
     * @return {@link String}
     */
    public static String getTableName(Class<?> interfaceClass) {
        Class<?> entityClass = ClassUtil.getGenericClass(interfaceClass);

        TableName myTable = entityClass.getAnnotation(TableName.class);
        if (myTable != null) {
            return myTable.value();
        } else {
            // 默认使用类名作为表名
            return entityClass.getSimpleName().toLowerCase();
        }
    }

}
